package org.cid54.bukkit;

import java.util.HashMap;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerListener;
import org.cid54.bukkit.command.AquamanCommand;
import org.cid54.bukkit.command.BluePrintCommand;
import org.cid54.bukkit.command.BuildCommand;
import org.cid54.bukkit.command.CilinderCommand;
import org.cid54.bukkit.command.ConnectCommand;
import org.cid54.bukkit.command.EmptyCommand;
import org.cid54.bukkit.command.EquipCommand;
import org.cid54.bukkit.command.ExportBluePrintCommand;
import org.cid54.bukkit.command.FindCommand;
import org.cid54.bukkit.command.GateCommand;
import org.cid54.bukkit.command.GiveCommand;
import org.cid54.bukkit.command.HealCommand;
import org.cid54.bukkit.command.HelpCommand;
import org.cid54.bukkit.command.KillCommand;
import org.cid54.bukkit.command.ReplaceCommand;
import org.cid54.bukkit.command.SpawnCommand;
import org.cid54.bukkit.command.SphereCommand;
import org.cid54.bukkit.command.util.PlayerCommand;
import org.cid54.bukkit.command.TelePortCommand;

/**
 *
 * @author Cid54
 */
public class Cid54PlayerListener extends PlayerListener {

    private Map<String, PlayerCommand> commands;
    private Map<String, PlayerChatEvent> commandLog;

    public Cid54PlayerListener() {
        this.commands = new HashMap<String, PlayerCommand>();

        this.addCommand(new CilinderCommand());
        this.addCommand(new ExportBluePrintCommand());
        this.addCommand(new HealCommand());
        this.addCommand(new BluePrintCommand());
        this.addCommand(new SphereCommand());
        this.addCommand(new ReplaceCommand());
        this.addCommand(new KillCommand());
        this.addCommand(new ReplaceCommand());
        this.addCommand(new HelpCommand());
        this.addCommand(new AquamanCommand());
        this.addCommand(new FindCommand());
        this.addCommand(new GiveCommand());
        this.addCommand(new GateCommand());
        this.addCommand(new TelePortCommand());
        this.addCommand(new ConnectCommand());
        this.addCommand(new BuildCommand());
        this.addCommand(new EquipCommand());
        this.addCommand(new EmptyCommand());
        this.addCommand(new SpawnCommand());

        this.commandLog = new HashMap<String, PlayerChatEvent>();

    }

    private void addCommand(PlayerCommand command) {
        this.commands.put(command.getCommand(), command);
    }

    public Map<String, PlayerCommand> getCommands() {
        return commands;
    }

    @Override
    public void onPlayerInteract(PlayerInteractEvent event) {
        super.onPlayerInteract(event);

        if (event.getPlayer().getItemInHand().getType().equals(Material.GOLD_HOE)) {
            if (event.getClickedBlock() != null) {
                event.getClickedBlock().setType(Material.AIR);
                event.setCancelled(true);
            }
        }
    }

    public static Location cloneLocation(Location l) {
        return new Location(l.getWorld(), l.getX(), l.getY(), l.getZ());
    }

    @Override
    public void onPlayerChat(PlayerChatEvent event) {
        super.onPlayerChat(event);

        boolean handled = false;

        if (event.getMessage().startsWith("!")) {

            if (event.getMessage().equals("!!")) {

                if (this.commandLog.containsKey(event.getPlayer().getName())) {

                    PlayerChatEvent command = this.commandLog.get(event.getPlayer().getName());

                    this.onPlayerChat(command);

                    handled = true;

                }

            } else {

                String command = event.getMessage().substring(1).toLowerCase();

                String key = command.split(" ")[0];

                PlayerCommand pc = this.commands.get(key);

                if (pc != null) {
                    handled = pc.execute(command, event, this);
                }

                if (handled) {

                    this.commandLog.put(event.getPlayer().getName(), event);

                }

            }

        }

        event.setCancelled(handled);

    }
}
